package Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2024-02-02
 * Time: 13:46
 */
public class MyQueue {
    static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode head;
    private ListNode tail;

    private int useSize;

    public boolean offer(int x) {
        ListNode node = new ListNode(x);
        if (head == null) {
            head = node;
            tail = node;
        } else {
            node.prev = tail;
            tail.next = node;
            tail = node;
        }
        useSize++;
        return true;
    }

    public int peek() {
        if (head == null) {
            return -1;
        }
        int val = head.val;
        return val;
    }

    public int poll() {
        if (head == null) {
            return -1;
        }
        int val = head.val;
        if (head.next == null) {
            head = null;
            tail = null;
            return val;
        }
        head = head.next;
        head.prev = null;
        useSize--;
        return val;
    }

    public boolean isEmpty() {
        return useSize == 0;
    }

    public int size() {
        return useSize;
    }
}
